import {core, getGameObjectById, HitTestScript, SpriteRenderer} from "../gameengine";
import {gameEndBtnPrefab, plotPrefab} from "../data/data";
import {marshal, MarshalDemoState, MarshalStartState} from "../marshal";
import { music } from "../music/music";

export class Plot {
    private startImages:string[]=[
        "assets/picture/plot/start1.png",
        "assets/picture/plot/start2.png",
        "assets/picture/plot/start3.png",
        "assets/picture/plot/start4.png",
        "assets/picture/plot/start5.png",
        "assets/picture/plot/start6.png",
        "assets/picture/plot/start7.png",
        "assets/picture/plot/start8.png",
        "assets/picture/plot/start9.png",
        "assets/picture/plot/start10.png",
        "assets/picture/plot/start11.png",
        "assets/picture/plot/start12.png"
    ];
    private end1Images:string[]=[
        "assets/picture/plot/fengzi_pic.png",
        "assets/picture/plot/fengzi_pic2.png",
        "assets/picture/plot/end1_1.png",
        "assets/picture/plot/end1_2.png"
    ];
    private end2Images:string[]=[
        "assets/picture/plot/fengzi_pic.png",
        "assets/picture/plot/fengzi_pic2.png",
        "assets/picture/plot/end2_1.png",
        "assets/picture/plot/end2_2.png"
    ];
    private end3Images:string[]=[
        "assets/picture/plot/fengzi_pic.png",
        "assets/picture/plot/end3_1.png",
        "assets/picture/plot/end3_2.png",
        "assets/picture/plot/end3_3.png",
        "assets/picture/plot/end3_4.png",
        "assets/picture/plot/end3_5.png",
        "assets/picture/plot/end3_6.png",
        "assets/picture/plot/end3_7.png"
       
    ];

    //win
    private end4Images:string[]=[
        "assets/picture/plot/end3_6.png",
        "assets/picture/plot/end3_7.png"
    ];
   

    private shockImages:string[]=[
        "assets/picture/plot/fengzi_pic.png",
        "assets/picture/plot/fengzi_pic2.png",
    ];

    private currentImages:string[];

    private successBtnUrl:string="assets/picture/button/btn_success.png";
    private killBtnUrl:string="assets/picture/button/btn_kill.png";

    private state:string="start";

    private play(){
        console.log("play");
        const container=getGameObjectById("Container");
        const prefab=core.createPrefab(plotPrefab);
        container.addChild(prefab);

        const spriteRender=prefab.getScript(SpriteRenderer);
        spriteRender.imageName=this.currentImages[0];

        const hitTest=prefab.getScript(HitTestScript);
        hitTest.onClick=()=>{
            console.log("click");
            const array=this.currentImages;
            const index=array.indexOf(spriteRender.imageName);

            if(index==array.length-1){
                if(this.state=="start"){
                    container.removeChild(prefab);
                }else {
                    this.buttonInstantiate();
                }
            }else {
                spriteRender.imageName=array[index+1];
            }
        }
    }

    public startPlot(){
        this.state="start";
        this.currentImages=this.startImages;
        console.log(plot);
        this.play();
    }

    //大门KILL结局
    public end1Plot(){
        this.state="end1";
        music.monsterPlay();
        // if (SpriteRenderer.imageName="fengzi_pic")
        // {
        //     music.monsterPlay();
        // }
        // else if(SpriteRenderer.imageName="fengzi_pic2")
        // {
        //     music.screamPlay();
        // }
        this.currentImages=this.end1Images;
        this.play();
    }

    //疯子KILL结局
    public end2Plot(){
        this.state="end2";
        music.monsterPlay();
        this.currentImages=this.end2Images;
        this.play();
    }

    //逃脱的结局
    public end3Plot(){
        this.state="end3";
        music.monsterPlay();
        this.currentImages=this.end3Images;
        this.play();
    }

  //win的结局
  public end4Plot(){
    this.state="end4";
    this.currentImages=this.end4Images;
    this.play();
}



    //吓人
    public shockPlot(){
        this.state="shock";
        music.monsterPlay();
        this.currentImages=this.shockImages;
        this.play();
    }

    private buttonInstantiate(){
        const container=getGameObjectById("Container");
        const prefab=core.createPrefab(gameEndBtnPrefab);
        container.addChild(prefab);

        const spriteRender=prefab.getScript(SpriteRenderer);
        if(this.state=="end3"){
            spriteRender.imageName=this.successBtnUrl;
        }else {
            spriteRender.imageName=this.killBtnUrl;
        }

        const hitTest=prefab.getScript(HitTestScript);
        hitTest.onClick=()=>{
            //切换到开始场景
            //开始场景请自己设定
            console.log("重新回到开始场景");
            marshal.changeState(new MarshalStartState());
        }
    }
}

export const plot=new Plot();